/*
 * vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4 foldmethod=marker:
 *
 * tools.c
 *
 * C.C.Allison
 * chris.allison@hotmail.com
 *
 * Started: Wednesday 21 November 2012, 10:46:01
 * Version: 0.00
 * Revision: $Id: tools.c 2 2013-01-14 05:39:03Z chris.charles.allison@gmail.com $
 * Last Modified: Saturday 12 January 2013, 04:00:47
 */

#include "tools.h"

struct tm *initTm(void)/* {{{1 */
{
    struct tm *tim;

    tim=(struct tm *) malloc(sizeof(struct tm));
    if(tim){
        tim->tm_sec=0;
        tim->tm_min=0;
        tim->tm_hour=0;
        tim->tm_mday=0;
        tim->tm_mon=0;
        tim->tm_year=0;
        tim->tm_wday=0;
        tim->tm_yday=0;
        tim->tm_isdst=0;
    }
    return tim;
}/* }}} */
char *bname(char *fqfilename)/* {{{1 */
{
    /*
     * returns a pointer to a new string
     * which is the basename of the 
     * fully qualified filename supplied
     */
    char *tmp;
    char *tbname=NULL;
    int len;

    tmp=basename(fqfilename);
    /* find out how big a string we need */
    len=snprintf(tbname,0,"%s",tmp);
    if((tbname=malloc(len)) == NULL){
        CCAE(1,"out of memory");
    }
    len=snprintf(tbname,len,"%s",tmp);
    return tbname;
}/* }}} */
long filesize(char *filename)/* {{{1 */
{
    /*
     * returns the filesize of filename if it can be read
     * returns -1 if an error occured.
     */
    int ret;
    long fsize=-1;
    struct stat *statbuf;

    statbuf=malloc(sizeof(struct stat));
    if(statbuf){
        /* syslog(LOG_DEBUG,"in filesize, malloced statbuf, statting"); */
        ret=stat(filename,statbuf);
        if(ret==0){
            /* syslog(LOG_DEBUG,"stat ok. reading file size"); */
            fsize=statbuf->st_size;
            // syslog(LOG_DEBUG,"file size: %ld",fsize);
        }else{
            syslog(LOG_ERR,"Cannot read file %s",filename);
        }
        /* syslog(LOG_DEBUG,"freeing stat buffer"); */
        free(statbuf);
    }else{
        syslog(LOG_ERR,"Cannot allocate memory for stat buffer");
    }
    /* syslog(LOG_DEBUG,"returning from filesize: %ld",fsize); */
    return fsize;
}/* }}} */
char *newstringpointer(char *str)/* {{{1 */
{
    /* don't use this, use strdup(3) instead */
    char *nsp;
    char *junk;
    int slen=0;
    slen=strlen(str);
    if((nsp=malloc(slen+1))){
        junk=strcpy(nsp,str);
        if(junk!=nsp){
            syslog(LOG_ERR,"failure in copying string.");
            free(nsp);
            nsp=NULL;
        }
    }else{
        syslog(LOG_ERR,"cannot allocate memory for new string pointer.");
    }
    return nsp;
}/* }}} */
char *numtostr(long long num)/* {{{1 */
{
    size_t length;
    char *buffer=NULL;
    length=snprintf(buffer,0,"%lld",num);
    length++;
    buffer=malloc(length);
    if(buffer){
        length=snprintf(buffer,length,"%lld",num);
        return buffer;
    }
    return NULL;
}/* }}} */
char *trim(char *str)/*{{{1*/
{
    char *string;
    string=chomp(str);
    string=ltrim(string);
    return rtrim(string);
}/*}}}*/
char *chomp(char *str) /* {{{1 */
{
    int len;
    int pos;

    len=strlen(str);
    pos=strcspn(str,"\n");
    if(pos<len){
        str[pos]='\0';
    }
    return str;
} /* }}} */
char *ltrim(char *str)/* {{{1 */
{
    char *string;
    string=ltrimsp(str);
    return ltrimt(string);
}/* }}} */
char *rtrim(char *str)/* {{{1 */
{
    char *string;
    string=rtrimsp(str);
    return rtrimt(string);
}/* }}} */
char *ltrimsp(char *str)/* {{{1 */
{
    return lefttrim(str,' ');
}/* }}} */
char *rtrimsp(char *str)/* {{{1 */
{
    return righttrim(str,' ');
}/*}}}*/
char *ltrimt(char *str)/*{{{1*/
{
    return lefttrim(str,'\t');
}/*}}}*/
char *rtrimt(char *str)/*{{{1*/
{
    return righttrim(str,'\t');
}/*}}}*/
char* lefttrim(char *string, char junk)/* {{{1 */
{
    char* original = string;
    char *p = original;
    int trimmed = 0;
    do
    {
        if (*original != junk || trimmed)
        {
            trimmed = 1;
            *p++ = *original;
        }
    }
    while (*original++ != '\0');
    return string;
}/* }}} */
char *righttrim(char *string, char junk)/* {{{1 */
{
    char *original = string + strlen(string);
    while (original != string && *--original == junk)
        ;

    if (*original != '\0')
        original[*original == junk ? 0 : 1] = '\0';
    return string;
}/* }}} */
